package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsFpf109eJhbzBo;
import com.ruoyi.tb.lmt.domain.vo.McgsFpf109eJhbzVo;
import com.ruoyi.tb.lmt.domain.McgsFpf109eJhbz;
import com.ruoyi.tb.lmt.mapper.McgsFpf109eJhbzMapper;
import com.ruoyi.tb.lmt.service.IMcgsFpf109eJhbzService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_分配阀109E均衡部组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsFpf109eJhbzServiceImpl implements IMcgsFpf109eJhbzService {

    private final McgsFpf109eJhbzMapper baseMapper;

    /**
     * 查询制动阀试验台_分配阀109E均衡部组_MCGS
     */
    @Override
    public McgsFpf109eJhbzVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_分配阀109E均衡部组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsFpf109eJhbzVo> queryPageList(McgsFpf109eJhbzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsFpf109eJhbz> lqw = buildQueryWrapper(bo);
        Page<McgsFpf109eJhbzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_分配阀109E均衡部组_MCGS列表
     */
    @Override
    public List<McgsFpf109eJhbzVo> queryList(McgsFpf109eJhbzBo bo) {
        LambdaQueryWrapper<McgsFpf109eJhbz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsFpf109eJhbz> buildQueryWrapper(McgsFpf109eJhbzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsFpf109eJhbz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsFpf109eJhbz::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsFpf109eJhbz::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsFpf109eJhbz::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsFpf109eJhbz::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsFpf109eJhbz::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsFpf109eJhbz::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsFh()), McgsFpf109eJhbz::getMcgsFh, bo.getMcgsFh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsFpf109eJhbz::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e1()), McgsFpf109eJhbz::getMcgsJhb109e1, bo.getMcgsJhb109e1());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e2()), McgsFpf109eJhbz::getMcgsJhb109e2, bo.getMcgsJhb109e2());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e3()), McgsFpf109eJhbz::getMcgsJhb109e3, bo.getMcgsJhb109e3());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e4()), McgsFpf109eJhbz::getMcgsJhb109e4, bo.getMcgsJhb109e4());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e5()), McgsFpf109eJhbz::getMcgsJhb109e5, bo.getMcgsJhb109e5());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJhb109e6()), McgsFpf109eJhbz::getMcgsJhb109e6, bo.getMcgsJhb109e6());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsKzc()), McgsFpf109eJhbz::getMcgsKzc, bo.getMcgsKzc());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsFpf109eJhbz::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsFpf109eJhbz::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsFpf109eJhbz::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsFpf109eJhbz::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsFpf109eJhbz::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsFpf109eJhbz::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsFpf109eJhbz::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_分配阀109E均衡部组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsFpf109eJhbzBo bo) {
        McgsFpf109eJhbz add = BeanUtil.toBean(bo, McgsFpf109eJhbz.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_分配阀109E均衡部组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsFpf109eJhbzBo bo) {
        McgsFpf109eJhbz update = BeanUtil.toBean(bo, McgsFpf109eJhbz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsFpf109eJhbz entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_分配阀109E均衡部组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }
}
